export default class SystemUtil {
  static isPC() {
    let userAgentInfo = navigator.userAgent;
    let agents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];
    let flag = true;
    for (let v = 0; v < agents.length; v++) {
      if (userAgentInfo.indexOf(agents[v]) > 0) {
        flag = false;
        break;
      }
    }
    return flag;
  }

  static getBrowserType() {
    let userAgentInfo = navigator.userAgent;
    if (/(iPhone|iPad|iPod|iOS)/i.test(userAgentInfo)) {
      return 'ios';
    }
    if (/(Android)/i.test(userAgentInfo)) {
      return 'android';
    }
    return 'pc';
  }

  static isWeixin() {
    let ua = navigator.userAgent.toLowerCase();
    let uaMatch = ua.match(/microMessenger/i);
    if (uaMatch === null) {
      return false;
    }

    return uaMatch.toString() === "micromessenger";
  }

  static isEmpty(value) {
    if (value === null || value === undefined) {
      return true;
    }

    let type = typeof value;
    if (type === 'boolean') {
      return value;
    }

    if (type === 'number') {
      return isNaN(value);
    }

    if (type === 'string') {
      return value === '';
    }

    if (type === 'object') {
      if (_.isArray(value)) {
        return value.length === 0;
      }
      return this.isEmptyObjct(value);
    }

    return false;
  }

  static isEmptyObjct(object) {
    for (let key in object) {
      return false;
    }
    return true;
  }

  static checkEmptyField(obj, fields) {
    for (let i = 0; i < fields.length; i++) {
      const field = fields[i];
      let value = obj[field];
      if (this.isEmpty(value)) {
        return field;
      }
    }
    return '';
  }

  static arrayEqual(arr1, arr2, strictEqual = true) {
    if (arr1.length !== arr2.length) {
      return false;
    }

    if (!strictEqual) {
      arr1 = _.orderBy(arr1);
      arr2 = _.orderBy(arr2);
    }

    return _.isEqual(arr1, arr2);
  }

  static getDataFromUrl(query) {
    if (query === '' || !query.startsWith('#')) {
      return null;
    }

    let obj = {};
    query = query.substring(1);
    let arr = query.split('&');
    _.forEach(arr, str => {
      let [key, value = ''] = str.split('=');
      obj[key] = value;
    });

    return obj;
  }

  static formatFormParam(obj, hasNull = true) {
    let paramList = [];
    for (let key in obj) {
      let value = obj[key];
      if (!hasNull && this.isEmpty(value)) {
        continue;
      }
      paramList.push(`${key}=${value}`);
    }

    let data = paramList.join('&');
    data = data.replace(/#/g, '%23').replace(/\?/g, '%3F');
    return data;
  }

  static getObjKeyByValue(object, value) {
    let key = _.findKey(object, v => v === value);
    return key;
  }

  static getId() {
    let s = [];
    let hexDigits = "0123456789abcdef";
    for (let i = 0; i < 32; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[12] = "4";
    s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);
    let uuid = s.join("");
    return uuid;
  }

  static sortCompare(obj1, obj2, field) {
    let value1 = obj1[field];
    let value2 = obj2[field];
    if (typeof value1 === 'string') {
      value1 = value1.trim();
    }
    if (typeof value2 === 'string') {
      value2 = value2.trim();
    }

    if (this.isEmpty(value1)) {
      value1 = null;
    }
    if (this.isEmpty(value2)) {
      value2 = null;
    }

    if (value1 === null || value2 === null) {
      if (value1 === value2) {
        return 0;
      }

      if (value1 === null) {
        return 1;
      } else {
        return -1;
      }
    }
    if (typeof value1 === 'string' || typeof value2 === 'string') {
      return value1.localeCompare(value2);
    } else {
      return value1 - value2;
    }
  }

  static getDataType(value) {
    let type = typeof value;
    if (type === 'object') {
      if (_.isArray(value)) {
        return 'array';
      }
    }
    return type;
  }

  static getBasePath() {
    let pathName = window.document.location.pathname;
    let projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    return projectName;
  }

  static getProjectName(){
    return '/cms';
  }
}
